//
//  ST_TrophyIntimacyCell.swift
//  btvideo
//
//  Created by mac on 2021/7/8.
//  Copyright © 2021 JarvanZhang. All rights reserved.
//

import UIKit
import SwiftyJSON
import SwiftyAttributes



class ST_TrophyIntimacyCell: UICollectionViewCell {
    
    override init(frame:CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .white
        
        [number_label,male_avatar_image_view,female_avatar_image_view,male_nickname_label,female_nickname_label1,male_sex_image_view,female_sex_image_view,widget,line].forEach{ contentView.addSubview($0) }
        
        number_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(15)
            make.centerY.equalToSuperview()
        }
        
        male_avatar_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(33)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(54)
        }
        
        female_avatar_image_view.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(71)
            make.centerY.equalToSuperview()
            make.width.height.equalTo(54)
        }
        
        male_nickname_label.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(135)
            make.width.lessThanOrEqualTo(ScreenWidth-270)
            make.top.equalToSuperview().offset(17)
            make.height.equalTo(20)
        }
        
        male_sex_image_view.snp.makeConstraints { (make) in
            make.left.equalTo(male_nickname_label.snp.right).offset(5)
            make.centerY.equalTo(male_nickname_label)
            make.width.height.equalTo(8)
        }
        
        female_nickname_label1.snp.makeConstraints { (make) in
            make.left.equalToSuperview().offset(135)
            make.top.equalToSuperview().offset(41)
            make.height.equalTo(20)
            make.width.lessThanOrEqualTo(ScreenWidth-270)
        }
        
        female_sex_image_view.snp.makeConstraints { (make) in
            make.left.equalTo(female_nickname_label1.snp.right).offset(5)
            make.centerY.equalTo(female_nickname_label1)
            make.width.height.equalTo(8)
        }
        
        widget.snp.makeConstraints { (make) in
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalToSuperview()
            make.height.equalTo(21)
            make.width.lessThanOrEqualTo(ScreenWidth-100)
        }
        
        line.snp.makeConstraints { (make) in
            make.right.bottom.equalToSuperview()
            make.height.equalTo(1)
            make.left.equalToSuperview().offset(33)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    lazy var number_label:UILabel = {
        let control = UILabel()
        control.text = ""
        control.textColor = #colorLiteral(red: 0.6745098039, green: 0.7058823529, blue: 0.7529411765, alpha: 1)
        control.font = UIFont.systemFont(ofSize: 13, weight: .medium)
        return control
    }()
    
    fileprivate lazy var male_nickname_label:UILabel = {
        let control = UILabel()
        control.textColor = #colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
        control.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        return control
    }()
    
    fileprivate lazy var female_nickname_label1:UILabel = {
        let control = UILabel()
        control.textColor = #colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
        control.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        return control
    }()
    
    fileprivate lazy var female_avatar_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 54/2.0
        control.borderWidth = 2
        control.borderColor = .white
        let tap = UITapGestureRecognizer(target: self, action: #selector(click(gez:)))
        control.isUserInteractionEnabled = true
        control.addGestureRecognizer(tap)
        return control
    }()
    
    fileprivate lazy var male_avatar_image_view:UIImageView = {
        let control = UIImageView()
        control.contentMode = .scaleAspectFill
        control.cornerRadius = 54/2.0
        control.borderWidth = 2
        control.borderColor = .white
        let tap = UITapGestureRecognizer(target: self, action: #selector(click(gez:)))
        control.isUserInteractionEnabled = true
        control.addGestureRecognizer(tap)
        return control
    }()
    
    fileprivate lazy var male_sex_image_view:UIImageView = {
        let control = UIImageView(image: ST_ImageUtils.imageName("TrophyIntimacyMale_icon"))
        return control
    }()
    
    fileprivate lazy var female_sex_image_view:UIImageView = {
        let control = UIImageView(image: ST_ImageUtils.imageName("TrophyIntimacyFemale_icon"))
        return control
    }()
    
    fileprivate lazy var widget:ST_TrophyValueWidget = {
       let control = ST_TrophyValueWidget()
        control.backgroundColor = #colorLiteral(red: 0.9960784314, green: 0.4509803922, blue: 0.4431372549, alpha: 0.1)
        return control
    }()
    
    fileprivate lazy var line:UIView = {
       let control = UIView()
        control.backgroundColor = #colorLiteral(red: 0.9607843137, green: 0.9725490196, blue: 0.9803921569, alpha: 1)
        return control
    }()
    
    weak var data:ST_TrophyIntimacyCellData?
}

extension ST_TrophyIntimacyCell{
    
    func bind(data:ST_TrophyIntimacyCellData,index:Int){
        
        self.data = data
        
        number_label.text = (index+4).string
        
        male_avatar_image_view.pin_setImage(from: data.manUser.avatar.compress_small_image(width: 48*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        male_nickname_label.text = data.manUser.nickname
        
        female_avatar_image_view.pin_setImage(from: data.womanUser.avatar.compress_small_image(width: 48*3).url, placeholderImage: ST_SwiftConst.shared.fetch_random_place_holder_image())
        
        female_nickname_label1.text = data.womanUser.nickname
        
        widget.title = data.intimacy
        
    }
    
    @objc fileprivate func click(gez:UITapGestureRecognizer){
        
        guard let d =  data else { return  }
        var accid:String
        if gez.view == male_avatar_image_view{
            accid = d.manUser.accid
            
            if d.manUser.rankClose == 2{
                self.parentViewController?.toast(text: "对方匿名，不能查看个人主页")
             return
            }
        }else{
            accid = d.womanUser.accid
            if d.womanUser.rankClose == 2{
                self.parentViewController?.toast(text: "对方匿名，不能查看个人主页")
             return
            }
        }
        
        let vc = ST_PairsCenterViewController()
        
        vc.accid = accid
        
        let nav = PairsCenterInteractiveNavigationController.init(rootViewController: vc)
        
        nav.overCurrentContext = true
        
        GlobalPresent(toController: nav)
        
        
    }
    
}


//MARK: - Data


class ST_TrophyIntimacyCellData : NSObject{

    var createTime : String
    var day : String
    var id : Int
    var intimacy : NSAttributedString
    var manMemberId : Int
    var manUser : ST_TrophyNormalCellUser
    var womanMemberId : Int
    var womanUser : ST_TrophyNormalCellUser

    /**
     * Instantiate the instance using the passed json values to set the properties values
     */
    init(fromJson json: JSON){
        
        createTime = json["create_time"].stringValue
        day = json["day"].stringValue
        id = json["id"].intValue
        
        let str = "亲密度：" +  String(format: "%.2f", json["intimacy"].floatValue)
        intimacy = str.withAttributes([Attribute.font(.systemFont(ofSize: 12)),.textColor(#colorLiteral(red: 0.9960784314, green: 0.4509803922, blue: 0.4431372549, alpha: 1))])
        
        
        manMemberId = json["man_member_id"].intValue
        manUser = ST_TrophyNormalCellUser(fromJson: json["man_user"])
        
        womanMemberId = json["woman_member_id"].intValue
        womanUser =  ST_TrophyNormalCellUser(fromJson: json["woman_user"])
    }
}
